package com.books.controller.elasticsearch;


import com.books.mapper.book.BookMapper;
import com.books.model.bo.Result;
import com.books.model.entity.book.Books;
import com.books.model.entity.shopping.ShoppingCar;
import com.books.service.book.IBookService;
import com.books.service.essearch.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


/**
 * @author ZhouRui
 * @title: 码上就来
 * @projectName cbstore-spring-boot
 * @description: TODO
 * @date 2021/10/2915:55
 */
@Controller
@RequestMapping
public class elasticsearchController {
    @Autowired
    private BookMapper booksRepository;

    @Autowired
    private IBookService bookService;

    @Autowired
    private RecommendService recommendService;

    @GetMapping("/recommedindex")
    public String recomed(){
        return "search/RecomSearch";
    }

    @GetMapping("/ESsearch")
    public String searchindex(String name, HttpServletRequest request){
        HttpSession session=request.getSession();
        List<Books> books;
        books=booksRepository.findByBookName(name);
        if(books.size()==0){
                books=booksRepository.findByBookCompay(name);
                if(books.size()==0){
                    books=booksRepository.findByAuthor(name);
                    if(books.size()==0){
                        books= null;
                        session.setAttribute("esBooks",books);
                        return "search/ESSearch";
                    }
                    session.setAttribute("esBooks",books);
                    return "search/ESSearch";
                }
                session.setAttribute("esBooks",books);
                return "search/ESSearch";
            }
        {
            session.setAttribute("esBooks",books);
            return "search/ESSearch";
        }
    }

    @GetMapping("/recommendsearch")
    public Result creates(HttpServletRequest request,String[] name,Integer uid){
        HttpSession session=request.getSession();
        List<Books> esBooks=new ArrayList<>();
        List<Integer> plists= recommendService.getRecommendItemsByUser(uid,20);
        for (int i = 0; i < plists.size(); i++) {
            Books books;
            books=bookService.getById(plists.get(i));
            if(books!=null){
                Books esBook=new Books();
                esBook.setStock(books.getStock());
                esBook.setDetail(books.getDetail());
                esBook.setDate(books.getDate());
                esBook.setCompay(books.getCompay());
                esBook.setAuthor(books.getAuthor());
                esBook.setPrice(books.getPrice());
                esBook.setCategoryId(books.getCategoryId());
                esBook.setPictureUrl(books.getPictureUrl());
                esBook.setTitle(books.getTitle());
                esBook.setBkId(books.getBkId());
                esBooks.add(esBook);
            }
        }
        session.setAttribute("RecomBooks",esBooks);
        return new Result("200","成功");
    }



    @GetMapping("/shoprecommed")
    public Result shoprecom(HttpServletRequest request,Integer uid){
        HttpSession session=request.getSession();
        List<Books> esBooks=new ArrayList<>();
        List<ShoppingCar> car;
        List<Integer> plists;
        HashMap<Integer,Books> map=new HashMap<>();
        List<Integer> list=new ArrayList<>();
        car= (List<ShoppingCar>) session.getAttribute("carlist");
        if(car!=null){
            for (int i = 0; i < car.size(); i++) {
                list.add(car.get(i).getBookId());
            }
            for (int j = 0; j < list.size(); j++) {
                plists= recommendService.getRecommendItemsByItem(uid, list.get(j), 5);
                for (int i = 0; i < plists.size(); i++) {
                    Books books;
                    books=bookService.getById(plists.get(i));
                    if(books!=null){
                        Books esBook=new Books();
                        esBook.setStock(books.getStock());
                        esBook.setDetail(books.getDetail());
                        esBook.setDate(books.getDate());
                        esBook.setCompay(books.getCompay());
                        esBook.setAuthor(books.getAuthor());
                        esBook.setPrice(books.getPrice());
                        esBook.setCategoryId(books.getCategoryId());
                        esBook.setPictureUrl(books.getPictureUrl());
                        esBook.setTitle(books.getTitle());
                        esBook.setBkId(books.getBkId());
                        map.put(esBook.getBkId(),esBook);
                    }
                }
            }
        }
        for (Books value : map.values()) {
            esBooks.add(value);
        }
        System.out.println(esBooks);
        session.setAttribute("RecomShop",esBooks);
        return new Result("200","成功");
    }
}

